package com.brush.brushbasicapi.mq.consumer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brush.brushcommon.common.basic.mapper.BankOfTopicMapper;
import com.brush.brushcommon.common.basic.mapper.CategoryOfSysquestionMapper;
import com.brush.brushcommon.common.basic.pojo.BankOfTopic;
import com.brush.brushcommon.common.basic.pojo.CategoryOfSysquestion;
import com.brush.brushcommon.common.basic.service.BankOfTopicService;
import com.brush.brushcommon.common.basic.service.CategoryOfSysquestionService;
import com.brush.brushcommon.common.sys.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

import static com.brush.brushcommon.common.mq.ExchangeConstants.*;

//消费者
@Component
@Slf4j
public class QuestionBankExchangeListener {

    @Resource
    private BankOfTopicMapper bankOfTopicMapper;

    @Resource
    private CategoryOfSysquestionMapper categoryOfSysquestionMapper;


    /**
     * @param ids
     * @return void
     * @author cws
     * @date 2024/8/3 20:50
     * @description 监听批量删除系统题库消息
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = QUEUE_DELETE_QUESTIONBANK,
                            durable = "true",
                            arguments = @Argument(name = "x-queue-mode", value = "lazy")
                    ),
                    exchange = @Exchange(name = EXCHANGE_DELETE_QUESTIONBANK, type = ExchangeTypes.DIRECT),
                    key = {ROUTING_KEY_DELETE_QUESTIONBANK}
            )
    )
    public void listenDirectQueue(List<String> ids) {
        try {
            log.info("MQ--批量删除问题库开始，QuestionBankId为: {}", Arrays.toString(ids.toArray()));
            Boolean result = bankOfTopicMapper.removeByQuestionBankIdBatch(ids);
            if (!result) {
                log.error("MQ--BankOfTopic表批量删除问题库失败，QuestionBankId为: {}", Arrays.toString(ids.toArray()));
            } else {
                log.info("MQ--BankOfTopic表批量删除问题库成功，QuestionBankId为: {}", Arrays.toString(ids.toArray()));
            }
        } catch (Exception e) {
            log.error("MQ--批量删除问题库失败: ", e);
//            抛出异常RepublishMessageRecoverer自动发送消息至error交换机
            throw e;
        }
    }


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = QUEUE_DELETE_CATEGORY,
                            durable = "true",
                            arguments = @Argument(name = "x-queue-mode", value = "lazy")
                    ),
                    exchange = @Exchange(name = EXCHANGE_DELETE_CATEGORY, type = ExchangeTypes.DIRECT),
                    key = {ROUTING_KEY_DELETE_CATEGORY}
            )
    )
    public void listenDirectQueueCategory(List<String> ids) {
        try {
            log.info("MQ---basic_category_of_sysquestion表批量删除问题库开始，CategoryId为: {}", Arrays.toString(ids.toArray()));
            Boolean result = categoryOfSysquestionMapper.removeByCategoryOfSysquestionBatch(ids);
            if (!result) {
                log.error("MQ---basic_category_of_sysquestion表批量删除问题库失败，CategoryId为: {}", Arrays.toString(ids.toArray()));
            } else {
                log.info("basic_category_of_sysquestion表批量删除问题库成功，CategoryId为: {}", Arrays.toString(ids.toArray()));
            }
        } catch (Exception e) {
            log.error("批量删除问题库失败: ", e);
            //            抛出异常RepublishMessageRecoverer自动发送消息至error交换机
            throw e;
        }
    }


}